package cn.ibizlab.codegen.groovy.support

import cn.ibizlab.codegen.groovy.util.JavaTypeUtil
import net.ibizsys.model.PSModelEnums.DEMethodInputType
import net.ibizsys.model.dataentity.IPSDataEntity
import net.ibizsys.model.dataentity.action.IPSDEActionInput
import net.ibizsys.model.dataentity.ds.IPSDEDataSetInput
import net.ibizsys.model.dataentity.service.IPSDEMethodInput

import java.sql.Timestamp

public class PSDEMethodInputExtension {

	public static String getJavaType(IPSDEMethodInput iPSDEMethodInput){
		
		DEMethodInputType returnType = DEMethodInputType.from(iPSDEMethodInput.getType())
		if(DEMethodInputType.NONE == returnType) {
			return ""
		}
		
		if(DEMethodInputType.DTOS == returnType) {
			
			if(iPSDEMethodInput instanceof IPSDEActionInput) {
				IPSDEActionInput iPSDEActionInput = (IPSDEActionInput)iPSDEMethodInput
				return String.format("List<%s>", iPSDEActionInput.getPSDEMethodDTOMust().getRealCodeName())
//				return String.format("List<IEntity>")
			}

			return "List"
		}
				
		if(DEMethodInputType.DTO == returnType) {
			
			if(iPSDEMethodInput instanceof IPSDEActionInput) {
				IPSDEActionInput iPSDEActionInput = (IPSDEActionInput)iPSDEMethodInput
				return iPSDEActionInput.getPSDEMethodDTOMust().getRealCodeName()
//				return "IEntity"
			}
			
			if(iPSDEMethodInput instanceof IPSDEDataSetInput) {
				IPSDEDataSetInput iPSDEDataSetInput = (IPSDEDataSetInput)iPSDEMethodInput
				return iPSDEDataSetInput.getPSDEFilterDTOMust().getRealCodeName()
//				return "ISearchContextDTO"
			}
			
			return "Object"
		}
		
		if(DEMethodInputType.FILTER == returnType) {
			
			if(iPSDEMethodInput instanceof IPSDEDataSetInput) {
				IPSDEDataSetInput iPSDEDataSetInput = (IPSDEDataSetInput)iPSDEMethodInput
				return iPSDEDataSetInput.getPSDEFilterDTOMust().getRealCodeName()
//				return "ISearchContextDTO"
			}
			
			return "Object"
		}
		
		if(DEMethodInputType.KEYFIELD == returnType || DEMethodInputType.KEYFIELDS == returnType ) {
			
			if(iPSDEMethodInput instanceof IPSDEActionInput) {
				IPSDEActionInput iPSDEActionInput = (IPSDEActionInput)iPSDEMethodInput
				IPSDataEntity iPSDataEntity = iPSDEActionInput.getParentPSModelObject(IPSDataEntity.class, false)
				String strJavaType = JavaTypeUtil.getJavaType(iPSDataEntity.getKeyPSDEFieldMust().getStdDataType())
				if(DEMethodInputType.KEYFIELDS == returnType) {
					if("Object".equals(strJavaType)) {
						return "List"
					}
					return String.format("List<%s>", strJavaType)
				}
				return strJavaType
			}
			return "Object"
		}

		
		return "Object"
	}

	static void main(String[] args) {
		Map map = new HashMap();
		map.put("a",1);
		map.put("e","3");

		map.put("b",null);
		map.put("d",new Date());
		String a=  map.get("a");
		String b=  map.get("b");
		String c=  map.get("c");
		Date d=  map.get("d");
		Integer e=  map.get("e");
		System.out.println(a);
		System.out.println(b);
		System.out.println(c);
		System.out.println(d);
		System.out.println(e);
	}
		
}
